var pswpElement;
var prefix = 'https://cdnjs.cloudflare.com/ajax/libs/photoswipe/5.4.4/';
var head = document.getElementsByTagName('head')[0];
['photoswipe', 'default-skin/default-skin'].forEach(function (href) {
	var link = document.createElement('link');
	link.rel = 'stylesheet';
	link.href = prefix + href + '.min.css';
	head.appendChild(link);
});
['photoswipe', 'photoswipe-ui-default'].forEach(function (src) {
	var script = document.createElement('script');
	script.src = prefix + src + '.min.js';
	head.appendChild(script);
});

var pswpElement;

axios.get('/wechat/pswp.xml').then(function (res) {
	var div = document.createElement('div');
	div.innerHTML = res.data;
	document.body.appendChild(div);
	pswpElement = document.getElementById('pswp');
});

function previewImg(pics, index) {
	new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, pics.map(function (src) {
		var img = new Image();
		img.src = src;
		return {
			src: src,
			w: img.width || 576,
			h: img.height || 576
		};
	}), {
		index: index
	}).init();
}

function uplaodImg(field, func) {
	var input = document.createElement('input');
	input.type = 'file';
	input.accept = 'image/*';
	input.onchange = function () {
		var fd = new FormData();
		fd.append('file', input.files[0]);
		var prefix = isTeacher() ? '/t' : '/s';
		axios.post(prefix + '/upload/' + field + '?schoolId=' +
			getPerson().schoolId, fd).then(function (res) {
				func(res.data.data);
				input.remove();
			});
	}
	input.click();
}